#!/bin/bash

job_initialize_target()
{
	# initialize error handling
	trap error_handler ERR

	msg_job_start "job_initialize_target"

	# recreate some needed dirs
	msg "regenerating special directories"  
	rm -v -rf ${mountpoint}/sys ${mountpoint}/proc ${mountpoint}/dev
	mkdir -p -v -m 1777 ${mountpoint}/tmp
	mkdir -p -v -m 1777 ${mountpoint}/var/tmp
	mkdir -p -v ${mountpoint}/var/log/old
	mkdir -p -v ${mountpoint}/var/lock/sane
	mkdir -p -v ${mountpoint}/boot/grub
	mkdir -p -v ${mountpoint}/usr/lib/locale
	mkdir -p -v ${mountpoint}/usr/share/icons/default
	mkdir -p -v ${mountpoint}/media
	mkdir -p -v ${mountpoint}/mnt
	mkdir -v ${mountpoint}/sys
	mkdir -v ${mountpoint}/proc

	# create basic devices on the target
	msg "creating basic devices"
	mkdir -v ${mountpoint}/dev
	mknod ${mountpoint}/dev/console c 5 1
	mknod ${mountpoint}/dev/null c 1 3
	mknod ${mountpoint}/dev/zero c 1 5

	# ensure correct permission on /tmp and /var/tmp
	msg "setting permissions on /tmp and /var/tmp"
	chmod -v 777 ${mountpoint}/var/tmp
	chmod -v o+t ${mountpoint}/var/tmp
	chmod -v 777 ${mountpoint}/tmp
	chmod -v o+t ${mountpoint}/tmp
	
	# install resolv.conf
	msg "copying /etc/resolv.conf"
	cp -vf /etc/resolv.conf ${mountpoint}/etc/resolv.conf
	
	# add basic configs for root
	msg "installing configs for root"
	cp -vf /etc/skel/.bashrc ${mountpoint}/root/.bashrc
	cp -vf /etc/skel/.bash_profile ${mountpoint}/root/.bash_profile
	cp -vf /etc/skel/.xinitrc ${mountpoint}/root/.xinitrc
	cp -vf /etc/skel/.xsession ${mountpoint}/root/.xsession
	cp -vf /etc/skel/local.conf ${mountpoint}/etc/fonts/local.conf

	# Set mouse theme for root
	msg "Setting mouse theme for root"
	cp -vf /usr/share/icons/default/index.theme ${mountpoint}/usr/share/icons/default/index.theme

	# Proper kdmrc
	sed -i -e 's~^.*Theme=/.*~Theme=/usr/share/apps/kdm/themes/ashoc~' ${mountpoint}/usr/share/config/kdm/kdmrc
	sed -i -e 's~^.*#AntiAliasing=.*~AntiAliasing=true~' ${mountpoint}/usr/share/config/kdm/kdmrc
	sed -i -e 's~^.*#TerminateServer=.*~TerminateServer=true~' ${mountpoint}/usr/share/config/kdm/kdmrc
	
	msg_job_done
}
